<template class="body">
  <div class="login_page">
    <div id="app">
      <el-card class="el-card">
        <template #header> 面经运营后台 </template>
        <el-form :rules="rules" ref="form" :model="login_form">
          <el-form-item label="用户名：" class="is-required" prop="username">
            <el-input v-model="login_form.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码：" class="is-required" prop="password">
            <el-input v-model="login_form.password" placeholder="请输入密码" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="login()">登录</el-button>
            <el-button @click="reset()">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
export default {
  name: 'LoginIndex',
  data() {
    return {
      login_form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 10, message: '用户名长度在 5 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 12, message: '密码长度在 5 到 12 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return
        }
        const res = await loginAPI(this.login_form)
        console.log(res);
        if (res.data.status == 200) {
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          this.$router.push('/')
        } else {
          this.$message.error(res.data.message);
        }

        // console.log(valid);
      })
    },
    reset() {
      this.$refs.form.resetFields()
    }
  },
}
</script>

<style lang="scss" scoped>
.login_page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: url('/src/assets/mj.jpg') center center fixed;
  background-size: 100% 100%;
  width: 100%;
  height: 100%;

}

.el-card {
  width: 420px;
  margin: 0 auto;

  ::v-deep .el-card__header {
    background: rgba(114, 124, 245, 1);
    color: #fff;
    text-align: center;
  }
}

.btn {
  text-align: center;
}
</style>
